package com.hqyj.springBoot.modules.account.controller;


import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.account.entity.User;
import com.hqyj.springBoot.modules.account.service.UserService;
import com.hqyj.springBoot.modules.common.entity.ResultEntity;
import com.hqyj.springBoot.modules.common.entity.SearchBean;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1/api/login ---- post
     * {"userName":"baiyun", "password":"123456"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    @Transactional
    public ResultEntity<User> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 127.0.0.1/api/user ---- post
     * {"userName":"baiyun", "password":"111111","email":"123456@qq.com","userImage":"/image/phoenix.jpg"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    @Transactional
    public ResultEntity<User> addUser(@RequestBody User user){
        return userService.addUser(user);
    }


    /**
     * 127.0.0.1/api/user ---- put
     * {"id":1,"userName":"admin", "password":"123456"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    @Transactional
    public ResultEntity<User> updateUserByUser(@RequestBody User user){
        return userService.updateUserByUser(user);
    }

    /**
     * 127.0.0.1/api/user/2 ---- delete
     */
    @DeleteMapping(value = "/user/{id}")
    @RequiresPermissions (value={"deleteUser"})
    public ResultEntity<User> deleteUserById(@PathVariable int id){
        return userService.deleteUserById(id);
    }

    /**
     *127.0.0.1/api/user/1 ---- get
     */
    @GetMapping("user/{id}")
    public User selectUserById(@PathVariable int id){
        return userService.selectUserById(id);
    }

    /**
     * 127.0.0.1/api/users ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> selectAllUser(@RequestBody SearchBean searchBean) {
        return userService.selectAllUser(searchBean);
    }
}
